<?php
/**
 * Created by PhpStorm.
 * User: zhaox
 * Date: 2018/1/4
 * Time: 15:41
 */
namespace App\Http\Controllers;

use App\Models\Area;
use App\Models\Scene;
use App\Models\SearchSum;
use Illuminate\Http\Request;

class AreaController extends Controller {
    public function index($id){
        $area = Area::findOrFail($id);
        return view('pages.area',['area'=>$area]);
    }
    public function getSearchData(Request $request){
        $word = $request['word'];
        $area = Area::where('name','like','%'.$word.'%')->first();
        if (isset($area)){
            $searchNum = SearchSum::where("area_id",$area->id)->first();
            if (is_null($searchNum)){
                $searchNum = new SearchSum();
                $searchNum->search_sum = 1;
                $searchNum->area_id = $area->id;
            }else{
                $searchNum->search_sum = $searchNum->search_sum+1;
            }
            $searchNum->save();
            return response()->json(['res'=>true,'url'=>'/index.php/area/'.$area->id]);
        }
        return response()->json(['res'=>true,'url'=>'/index.php/error']);
    }
    public function getWrod(){
        $searchWords = SearchSum::orderBy("search_sum",'desc')->paginate(7);
        $wordArr = [];
        foreach($searchWords as $searchWord){
            array_push($wordArr,$searchWord->getArea->name);
        }
        return response()->json(['res'=>true,'searchWords'=>$wordArr]);
    }
    public function getSceneList(Request $request){
        $areaId = $request['areaId'];
        $scenes = Scene::where("area_id",$areaId)->paginate(8);
        $sideScene = [];
        foreach ($scenes as $scene){
            $arr = [];
            $arr['img']  = $scene->cover;
            $arr['name'] = $scene->name;
            $arr['url']  = '/index.php/scene/'.$scene->id;
            array_push($sideScene,$arr);
        }
        return response()->json(['res'=>true,'scenes'=>$sideScene]);
    }
}